package com.taotao.admin.rest;

import com.rop.client.CompositeResponse;
import com.taotao.admin.MyRopClient;
import com.taotao.admin.pojo.TableUI;
import com.taotao.rop.database.Constant;
import com.taotao.rop.database.domain.WechatResp;
import com.taotao.rop.response.BaseResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/wechat/resp")
public class WechatRespController {
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Long pageSize = Long.parseLong(request.getParameter("pageSize"));
        Long pageNo = Long.parseLong(request.getParameter("pageNo"));
        String content = request.getParameter("content");
        String keyword = request.getParameter("keyword");
        String keyvalue = request.getParameter("keyvalue");
        String type = request.getParameter("type");

        WechatResp condition = new WechatResp();
        condition.setPageSize(pageSize);
        condition.setPageNo(pageNo);
        if (StringUtils.isNotBlank(content)) {
            condition.setContent("%"+content+"%");
        }
        if (StringUtils.isNotBlank(keyword)) {
            condition.setKeyword("%"+keyword+"%");
        }
        if (StringUtils.isNotBlank(keyvalue)) {
            condition.setKeyvalue(keyvalue);
        }
        if (StringUtils.isNotBlank(type)) {
            condition.setType(type);
        }

        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(condition, BaseResponse.class, "wechatResp.list", "1.0");
        if (response.isSuccessful()){
            BaseResponse resp = response.getSuccessResponse();
            if (resp.getSuccess()){
                TableUI tableUI = new TableUI();
                List<LinkedHashMap<String, Object>> respData = (List<LinkedHashMap<String, Object>>) resp.getData();
                tableUI.setTotal(resp.getMsg());
                tableUI.setRows(respData);
                return tableUI;
            } else {
                return resp;
            }
        } else {
            return response.getErrorResponse();
        }
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        WechatResp wechatResp = null;
        try {
            wechatResp = getWechatRespFromRequest(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (wechatResp != null) {
            CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                    .post(wechatResp, BaseResponse.class, "wechatResp.insert", "1.0");
            if (response.isSuccessful()){
                return response.getSuccessResponse();
            } else {
                return response.getErrorResponse();
            }
        } else {
            BaseResponse resp = new BaseResponse();
            resp.setMsg("输入有误，请核对");
            return resp;
        }
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request, @PathVariable String id) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        WechatResp wechatResp = null;
        try {
            wechatResp = getWechatRespFromRequest(request);
            wechatResp.setId(Long.parseLong(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (wechatResp != null) {
            CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                    .post(wechatResp, BaseResponse.class, "wechatResp.update", "1.0");
            if (response.isSuccessful()){
                return response.getSuccessResponse();
            } else {
                return response.getErrorResponse();
            }
        } else {
            BaseResponse resp = new BaseResponse();
            resp.setMsg("输入有误，请核对");
            return resp;
        }
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(HttpServletRequest request, @PathVariable String id) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        WechatResp wechatResp = null;
        try {
            wechatResp = getWechatRespFromRequest(request);
            wechatResp.setId(Long.parseLong(id));
            wechatResp.setStatus(Constant.STATUS_DELETE);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (wechatResp != null) {
            CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                    .post(wechatResp, BaseResponse.class, "wechatResp.update", "1.0");
            if (response.isSuccessful()){
                return response.getSuccessResponse();
            } else {
                return response.getErrorResponse();
            }
        } else {
            BaseResponse resp = new BaseResponse();
            resp.setMsg("输入有误，请核对");
            return resp;
        }
    }

    private WechatResp getWechatRespFromRequest(HttpServletRequest request) throws Exception {
        String msgType = request.getParameter("msgType");
        String content = request.getParameter("content");
        String title = request.getParameter("title");
        String appId = request.getParameter("appId");
        String status = request.getParameter("status");
        String keyword = request.getParameter("keyword");
        String priority = request.getParameter("priority");
        String keyvalue = request.getParameter("keyvalue");
        String shortTitle = request.getParameter("shortTitle");
        String isMenu = request.getParameter("isMenu");

        WechatResp wechatResp = new WechatResp();
        wechatResp.setMsgType(msgType);
        wechatResp.setContent(content);
        wechatResp.setTitle(title);
        wechatResp.setAppId(appId);
        if (status != null) {
            wechatResp.setStatus(Integer.parseInt(status));
        }
        wechatResp.setKeyword(keyword);
        if (priority != null) {
            wechatResp.setPriority(Integer.parseInt(priority));
        }
        wechatResp.setKeyvalue(keyvalue);
        wechatResp.setShortTitle(shortTitle);
        if (StringUtils.isNotBlank(isMenu)) {
            if (StringUtils.isNumeric(isMenu)) {
                wechatResp.setIsMenu(Integer.parseInt(isMenu));
            } else if ("on".equalsIgnoreCase(isMenu)) {
                wechatResp.setIsMenu(1);
            } else {
                wechatResp.setIsMenu(0);
            }
        } else {
            wechatResp.setIsMenu(0);
        }
        return wechatResp;
    }

    @RequestMapping(value = "/getDefault", method = RequestMethod.POST)
    @ResponseBody
    public Object getDefault(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        WechatResp wechatResp = new WechatResp();
        String keyvalue = request.getParameter("keyvalue");
        if (StringUtils.isNotBlank(keyvalue)) {
            wechatResp.setKeyvalue(keyvalue);
        }
        wechatResp.setAppId(MyRopClient.APP_ID);
        wechatResp.setStatus(Constant.STATUS_ENABLE);
        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(wechatResp, BaseResponse.class, "wechatResp.find", "1.0");
        if (response.isSuccessful()){
            return response.getSuccessResponse();
        } else {
            return response.getErrorResponse();
        }
    }
}
